import 'package:flutter/material.dart';
import 'package:flutter_dk_form/flutter_dk_form.dart';
import 'package:dk_flutter_base/dk_core.dart';
import 'dart:convert';

class FollowUpDetailPage extends StatefulWidget {
  static const String routeName = '/page/follow_up_detail_page';
  static const String name = "FollowUpDetailPage";

  @override
  _FollowUpDetailState createState() => _FollowUpDetailState();
}

class _FollowUpDetailState extends State<FollowUpDetailPage> {
  GlobalKey<DkFormState> _formKey = GlobalKey<DkFormState>();

  void _forSubmitted() {
    var _form = _formKey.currentState;

    if (_form.validate()) {
      _form.save();
      print(json.encode(_form.value));
    } else {
      print(_form.errorText);
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          title: Text('房源跟进详情'),
        ),
        body: Column(
          children: <Widget>[
            Expanded(
              child: DkForm(
                key: _formKey,
                child: _Detail(),
              ),
            ),
            BottomButtonWidget(
              onCommit: _forSubmitted,
            ),
          ],
        ),
      ),
    );
  }
}

class _Detail extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        DkSingleInputFormField(
          attribute: 'id',
          title: '房源ID',
          placeholder: '请输入渠道推送的房源ID',
          validator: (value) {
            return (stringBlank(value)) ? '必须输入房源ID' : null;
          },
        ),
        DkSwitchFormField(
          initialValue: true,
          attribute: 'close',
          title: '关闭任务',
        ),
        DkDropDownFormField(
          attribute: 'reason',
          title: '关闭原因',
          placeholder: '请选择关闭任务原因',
          list: [
            DkDropDownItem(1, '无法获取房源信息'),
            DkDropDownItem(2, '房源已出租'),
            DkDropDownItem(3, '房源已存在')
          ],
          validator: (value) {
            return value == null ? '必须选择关闭原因' : null;
          },
        ),
      ],
    );
  }
}
